#include "communication.h"
#ifdef _WIN32
u8 recvPacket(DP *packet)
{
    char buf[4096];
    char *ptr = buf;
    int size = sizeof(packet->sa);
    int len = recvfrom(sock, buf, sizeof buf, 0, &packet->sa, &size);
    if (len == 0)
        return 0;
    memcpy(&packet->head, buf, sizeof(DH));
    ptr += sizeof(DH);
    u16 len = ntohs(packet->head.qdcount);
    packet->qd = malloc(len * sizeof(QS));
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        packet->qd[i].qname = malloc(l + 1);
        memcpy(packet->qd[i].qname, ptr, l + 1);
        ptr += l + 1;
        packet->qd[i].qtype = *(u16 *)ptr;
        ptr += 2;
        packet->qd[i].qclass = *(u16 *)ptr;
        ptr += 2;
    }
    len = ntohs(packet->head.ancount);
    packet->an = malloc(len * sizeof(RR));
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        packet->an[i].domain = malloc(l + 1);
        memcpy(packet->an[i].domain, ptr, l + 1);
        ptr += l + 1;
        packet->an[i].rtype = *(u16 *)ptr;
        ptr += 2;
        packet->an[i].rclass = *(u16 *)ptr;
        ptr += 2;
        packet->an[i].ttl = *(u32 *)ptr;
        ptr += 4;
        packet->an[i].rdatalength = *(u16 *)ptr;
        ptr += 2;
        packet->an[i].rdata = malloc(ntols(packet->an[i].rdatalength));
        memcpy(packet->an[i].rdata, ptr, ntols(packet->an[i].rdatalength));
        ptr += packet->an[i].rdatalength;
    }
    len = ntohs(packet->head.nscount);
    packet->an = malloc(len * sizeof(RR));
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        packet->ns[i].domain = malloc(l + 1);
        memcpy(packet->ns[i].domain, ptr, l + 1);
        ptr += l + 1;
        packet->ns[i].rtype = *(u16 *)ptr;
        ptr += 2;
        packet->ns[i].rclass = *(u16 *)ptr;
        ptr += 2;
        packet->ns[i].ttl = *(u32 *)ptr;
        ptr += 4;
        packet->ns[i].rdatalength = *(u16 *)ptr;
        ptr += 2;
        packet->ns[i].rdata = malloc(ntols(packet->ns[i].rdatalength));
        memcpy(packet->ns[i].rdata, ptr, ntols(packet->ns[i].rdatalength));
        ptr += packet->ns[i].rdatalength;
    }
    packet->ar = malloc(len * sizeof(RR));
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        packet->ar[i].domain = malloc(l + 1);
        memcpy(packet->ar[i].domain, ptr, l + 1);
        ptr += l + 1;
        packet->ar[i].rtype = *(u16 *)ptr;
        ptr += 2;
        packet->ar[i].rclass = *(u16 *)ptr;
        ptr += 2;
        packet->ar[i].ttl = *(u32 *)ptr;
        ptr += 4;
        packet->ar[i].rdatalength = *(u16 *)ptr;
        ptr += 2;
        packet->ar[i].rdata = malloc(ntols(packet->ar[i].rdatalength));
        memcpy(packet->ar[i].rdata, ptr, ntols(packet->ar[i].rdatalength));
        ptr += packet->ar[i].rdatalength;
    }
    return 1;
}

u8 sendPacket(DP *packet)
{
    if (packet == NULL)
        return 0;
    char buf[4096], *ptr;
    ptr = buf;
    memcpy(buf, &packet->head, sizeof(DH));
    ptr += sizeof(DH);
    u16 len = ntohs(packet->head.qdcount);
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        memcpy(ptr, packet->qd[i].qname, l + 1);
        ptr += l + 1;
        memcpy(ptr, &packet->qd[i].qtype, 2);
        ptr += 2;
        memcpy(ptr, &packet->qd[i].qclass, 2);
        ptr += 2;
    }
    len = ntohs(packet->head.ancount);
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        memcpy(ptr, packet->an[i].domain, l + 1);
        ptr += l + 1;
        memcpy(ptr, &packet->an[i].rtype, 2);
        ptr += 2;
        memcpy(ptr, &packet->an[i].rclass, 2);
        ptr += 2;
        memcpy(ptr, &packet->an[i].ttl, 4);
        ptr += 4;
        memcpy(ptr, &packet->an[i].rdatalength, 2);
        ptr += 2;
        memcpy(ptr, packet->an[i].rdata, ntols(packet->an[i].rdatalength));
        ptr += packet->an[i].rdatalength;
    }
    len = ntohs(packet->head.nscount);
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        memcpy(ptr, packet->ns[i].domain, l + 1);
        ptr += l + 1;
        memcpy(ptr, &packet->ns[i].rtype, 2);
        ptr += 2;
        memcpy(ptr, &packet->ns[i].rclass, 2);
        ptr += 2;
        memcpy(ptr, &packet->ns[i].ttl, 4);
        ptr += 4;
        memcpy(ptr, &packet->ns[i].rdatalength, 2);
        ptr += 2;
        memcpy(ptr, packet->ns[i].rdata, ntols(packet->an[i].rdatalength));
        ptr += packet->ns[i].rdatalength;
    }
    len =  ntohs(packet->head.arcount);
    for (u16 i = 0; i < len; i++)
    {
        int l = strlen(ptr);
        memcpy(ptr, packet->ar[i].domain, l + 1);
        ptr += l + 1;
        memcpy(ptr, &packet->ar[i].rtype, 2);
        ptr += 2;
        memcpy(ptr, &packet->ar[i].rclass, 2);
        ptr += 2;
        memcpy(ptr, &packet->ar[i].ttl, 4);
        ptr += 4;
        memcpy(ptr, &packet->ar[i].rdatalength, 2);
        ptr += 2;
        memcpy(ptr, packet->ar[i].rdata, ntols(packet->an[i].rdatalength));
        ptr += packet->ar[i].rdatalength;
    }
    sendto(sock,buf,ptr-buf,0,&packet->sa,sizeof (packet->sa));
    return 1;
}
#else
u8 recvPacket(DP *packet)
{
}

u8 sendPacket(DP *packet)
{
}
#endif
